package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.ReportMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.comment.Report;
import com.qingcheng.service.comment.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Report> findAll() {
        return reportMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Report> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Report> reports = (Page<Report>) reportMapper.selectAll();
        return new PageResult<Report>(reports.getTotal(),reports.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Report> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return reportMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Report> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Report> reports = (Page<Report>) reportMapper.selectByExample(example);
        return new PageResult<Report>(reports.getTotal(),reports.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Report findById(String id) {
        return reportMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param report
     */
    public void add(Report report) {
        reportMapper.insert(report);
    }

    /**
     * 修改
     * @param report
     */
    public void update(Report report) {
        reportMapper.updateByPrimaryKeySelective(report);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(String id) {
        reportMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Report.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // id
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
            }
            // 举报用户
            if(searchMap.get("username")!=null && !"".equals(searchMap.get("username"))){
                criteria.andLike("username","%"+searchMap.get("username")+"%");
            }
            // 原因1；黄赌毒 2.晒图片与商品不符
            if(searchMap.get("reason")!=null && !"".equals(searchMap.get("reason"))){
                criteria.andLike("reason","%"+searchMap.get("reason")+"%");
            }
            // 评论Id
            if(searchMap.get("commentId")!=null && !"".equals(searchMap.get("commentId"))){
                criteria.andLike("commentId","%"+searchMap.get("commentId")+"%");
            }
            // 举报详情
            if(searchMap.get("message")!=null && !"".equals(searchMap.get("message"))){
                criteria.andLike("message","%"+searchMap.get("message")+"%");
            }


        }
        return example;
    }

}
